<template>
    <el-main>
        <!--搜索栏-->
        <el-form :model="listParm"  label-width="80px" :inline="true" size="default">
            <el-form-item>
                <el-input v-model="listParm.name" placeholder="姓名"></el-input>
            </el-form-item>
            <el-form-item>
                <el-input v-model="listParm.phone" placeholder="电话"></el-input>
            </el-form-item>
            <el-form-item>
                <el-input v-model="listParm.username" placeholder="用户名"></el-input>
            </el-form-item>
            <el-form-item></el-form-item>
            <el-form-item>
                <el-button type="success" :icon="Search" @click="searchBtn">搜索</el-button>
                <el-button type="warning" :icon="Close" @click="resetBtn">重置</el-button>
                <el-button type="primary" :icon="Plus" @click="addBtn">新增</el-button>
            </el-form-item>
        </el-form> 
        <!--用户列表-->
        <el-table :height="tableHeight" :data="tableList" border stripe>
            <el-table-column label="姓名" prop="name"></el-table-column>
            <el-table-column label="性别" prop="gender">
                <template #default="scope">
                    <el-tag v-if="scope.row.gender == '1'" type="warning" size="default"  >女</el-tag>
                    <el-tag v-else="scope.row.gender == '0'" size="default"  >男</el-tag>
                </template>
            </el-table-column>
            <el-table-column label="用户名" prop="username"></el-table-column>
            <el-table-column label="密码" prop="password"></el-table-column>
            <el-table-column label="电话" prop="phone"></el-table-column>
            <el-table-column label="操作" width="200">
                <template #default="scope">
                    <el-button :icon="Edit" type="primary" size="default" @click="editBtn(scope.row)">编辑</el-button>
                    <el-button :icon="Delete" type="danger" size="default" @click="deleteBtn(scope.row)">删除</el-button>
                </template>
            </el-table-column>
        </el-table>
        <!--分页-->
        <el-pagination
            @size-change="sizeChange"
            @current-change="currentChange"
            :current-page.sync="listParm.page"
            layout="total, prev, pager, next, jumper"
            :total="listParm.totalSize" background>
        </el-pagination>
        
    </el-main>
    <!--新增、编辑的弹框-->
    <AddUser ref="addRef" @onFresh="getList"></AddUser>
</template>

<script setup lang="ts">
import AddUser from './AddUser.vue';
import { Search, Close, Plus, Edit, Delete} from '@element-plus/icons-vue'
import useUserTable from '@/module/user/useUserTable';
import useUser from '@/module/user/useUser';
import { onMounted } from 'vue';
import { useRouter } from 'vue-router';
import { userStore } from '@/store/user';
const router = useRouter();
const store = userStore();
onMounted(() => {
    store.autoLogin(); // 尝试自动登录
    if (store.$state.key != '' && store.$state.key) {
        router.push('/user');
    } else {
        router.push('/login');
    }
});
//表格相关操作
const { tableHeight,listParm,getList,searchBtn,resetBtn,tableList,sizeChange,currentChange } = useUserTable();
//用户的增删改操作
const { addBtn,editBtn,deleteBtn,addRef } = useUser(getList);
</script>

<style scoped>

</style>